package com.depository.oss.quere;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author xiaomei
 * @version V1.0
 * @Title: SendEmailQueue
 * @Package com.xiaomei.queue.redis.demo01
 * @Description:
 * @date 11/7/17
 */
public class SendEmailQueue implements Runnable{

    //@Autowired
    private IRedisQueue<EmailEntity> redisQueue;


    public IRedisQueue<EmailEntity> getRedisQueue() {
        return redisQueue;
    }

    public void setRedisQueue(IRedisQueue<EmailEntity> redisQueue) {
        this.redisQueue = redisQueue;
    }


    public void sentEmail(EmailEntity emailEntity) {
        redisQueue.addOpposite(emailEntity);
    }

    public EmailEntity getEmail() throws InterruptedException {
        return redisQueue.poll(1);
    }


    @Override
    public void run() {
        try {
            while (true){
                System.out.println("take"+getEmail());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:config/redis/spring-redis.xml");
        SendEmailQueue sendEmailQueue = (SendEmailQueue) applicationContext.getBean("sentEmailQueue");
        Thread thread = new Thread(){
            @Override
            public void run() {
                for (int i = 1 ; i <101 ; i++){
                    EmailEntity emailEntity = new EmailEntity();
                    emailEntity.setEmailAddr(i+"-- - @qq.com");
                    emailEntity.setUserName("name:"+i);
                    sendEmailQueue.sentEmail(emailEntity);
                }
            }
        };
        thread.run();
        sendEmailQueue.run();
    }
}
